#include "stdafx.h"

#include "frontend.h"
#include "finance.h"

#include "defaultview.h"
#include "ViewLoaderHelper.h"
#include "roomlist.h"
#include "SystemConfig.h"
#include "CommonWindow.h"

#include "Customer.h"



#define DEFAULTVIEW_INDEX 0
#define FRONTENDVIEW_INDEX 1
#define FINANCEVIEW_INDEX 2
#define NEWSVIEW_INDEX 3
#define ROOMLISTVIEW_INDEX 4
#define SCHEDULEVIEW_INDEX 5
#define SYSTEMCONFIGVIEW_INDEX 6
#define CUSTOMERVIEW_INDEX 7
#define ROOMSTATUSVIEW_INDEX 8
#define VIEW_MAX 9
ViewLoader::ViewLoader(HWND hWnd)
{
	typedef View *PtrView;
	m_hWnd=hWnd;
	m_pView = new PtrView[VIEW_MAX];
	memset(m_pView, NULL, VIEW_MAX*sizeof(PtrView));
	CommonWindow* pWindow=new CommonWindow();

	pWindow->RegisterWindow();
}
int ViewLoader::LoadFrontEndView()
{
	if(m_pView[FRONTENDVIEW_INDEX]==NULL)
	{
		HideAll();
		FrontEndView* pFEV=new FrontEndView();
		m_pView[FRONTENDVIEW_INDEX]=pFEV;
		m_pView[FRONTENDVIEW_INDEX]->Init(m_hWnd);
	}
	else
	{
		WindowToTop(FRONTENDVIEW_INDEX);
	}
	return 0;
}
int ViewLoader::LoadFinanceView()
{
	if(m_pView[FINANCEVIEW_INDEX]==NULL)
	{
		HideAll();
		Finance* pFV=new Finance();
		pFV->RegisterFinanceWindow();
		m_pView[FINANCEVIEW_INDEX]=pFV;
		pFV->Init(m_hWnd);
	}
	else
	{
		WindowToTop(FINANCEVIEW_INDEX);
	}
	return 0;
}

int ViewLoader::LoadScheduleView()
{
	CreateWindow(WC_BUTTON, "LoadScheduleView", WS_VISIBLE | WS_OVERLAPPEDWINDOW, 100, 100, 200, 200, NULL, NULL, GetModuleHandle(NULL), NULL);
	return 0;
}

int ViewLoader::LoadRoomListView()
{
	if(m_pView[ROOMLISTVIEW_INDEX]==NULL)
	{
		HideAll();
		m_pView[ROOMLISTVIEW_INDEX]=new RoomListView();
		m_pView[ROOMLISTVIEW_INDEX]->Init(m_hWnd);
	}
	else
	{
		WindowToTop(ROOMLISTVIEW_INDEX);
	}
	return 0;
}


int ViewLoader::LoadDefaultView()
{
	if(m_pView[DEFAULTVIEW_INDEX]==NULL)
	{
		HideAll();
		m_pView[DEFAULTVIEW_INDEX]=new DefaultView(m_hWnd);
		m_pView[DEFAULTVIEW_INDEX]->Init(m_hWnd);
	}
	else
	{
		WindowToTop(DEFAULTVIEW_INDEX);
	}
	return 0;
}

View* ViewLoader::GetDefaultView()
{
	return m_pView[DEFAULTVIEW_INDEX];
}


int ViewLoader::LoadSystemConfig()
{
	if(m_pView[SYSTEMCONFIGVIEW_INDEX]==NULL)
	{
		HideAll();
		m_pView[SYSTEMCONFIGVIEW_INDEX]=new SystemConfigView();
		SystemConfigView* pSysConfigView=static_cast<SystemConfigView*>(m_pView[SYSTEMCONFIGVIEW_INDEX]);
		pSysConfigView->RegisterSysClass();
		m_pView[SYSTEMCONFIGVIEW_INDEX]->Init(m_hWnd);
	}
	else
	{
		WindowToTop(SYSTEMCONFIGVIEW_INDEX);
	}
	return 0;
}

int ViewLoader::LoadCustomerView()
{
	if(m_pView[CUSTOMERVIEW_INDEX]==NULL)
	{
		HideAll();
		m_pView[CUSTOMERVIEW_INDEX]=new CustomerView();
		CustomerView* pCustomerView=static_cast<CustomerView*>(m_pView[CUSTOMERVIEW_INDEX]);
		pCustomerView->RegisterCmClass();
		m_pView[CUSTOMERVIEW_INDEX]->Init(m_hWnd);
	}
	else
	{
		WindowToTop(CUSTOMERVIEW_INDEX);
	}
	return 0;
}

int ViewLoader::HideAll()
{
	int i=1;
	for(;i<VIEW_MAX;i++)
	{
		if(m_pView[i]!=NULL)
			m_pView[i]->Hide();
	}
	return TRUE;
}

int ViewLoader::WindowToTop(int index)
{
	int i=1;
	for(;i<VIEW_MAX;i++)
	{
		if(i!=index && m_pView[i]!=NULL)
			m_pView[i]->Hide();
	}
	m_pView[index]->Show();
	return TRUE;
}

int ViewLoader::DestroyView(int index)
{
	m_pView[index]->Destroy();
	delete m_pView[index];
	m_pView[index]=NULL;
	return 0;
}


int ViewLoader::DestroyFrontEndView()
{
	DestroyView(FRONTENDVIEW_INDEX);
	return 0;
}

int ViewLoader::DestroyFinanceView()
{
	DestroyView(FINANCEVIEW_INDEX);
	return 0;
}

int ViewLoader::DestroySystemConfigView()
{
	DestroyView(SYSTEMCONFIGVIEW_INDEX);
	return 0;
}

int ViewLoader::DestroyCumView()
{
	DestroyView(CUSTOMERVIEW_INDEX);
	return 0;
}


int ViewLoader::DestroyRoomView()
{
	DestroyView(ROOMLISTVIEW_INDEX);
	return 0;
}

int ViewLoader::LoadRoomStatusView()
{
	CreateWindow(WC_BUTTON, "RoomStatus", WS_VISIBLE | WS_OVERLAPPEDWINDOW, 100, 100, 200, 200, NULL, NULL, GetModuleHandle(NULL), NULL);
	return 0;
}